package com.sz.main;

import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.example.gcord.gcorddemo.R;

import cn.com.geartech.gcordsdk.GcordSDK;
import cn.com.geartech.gcordsdk.PhoneAPI;

public class InCallActivity extends AppCompatActivity {


    public static final String CALL_TYPE = "call_type";
    public static final String CALL_TYPE_IN = "call_type_in";
    public static final String CALL_TYPE_OUT = "call_type_out";

    public static final String CALL_NUMBER = "call_number";

    Button btn_answer;
    Button btn_reject;
    Button btn_hangOff;
    TextView text_info;

    boolean isIncomingCall = false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_in_call);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                        .setAction("Action", null).show();
            }
        });


        initView();

        String call_type = getIntent().getStringExtra(CALL_TYPE);
        if(call_type != null && call_type.equals(CALL_TYPE_IN))
        {
            isIncomingCall = true;
            playRing();
            switchToAnswerMode();
        }
        else
        {
            switchToInCallMode();
        }

    }

    @Override
    protected void onResume() {
        super.onResume();
        GcordSDK.getInstance().getPhoneAPI().addPhoneEventListener(phoneEventListener);

    }

    @Override
    protected void onPause() {
        super.onPause();

        GcordSDK.getInstance().getPhoneAPI().removePhoneEventListener(phoneEventListener);

    }

    void switchToAnswerMode()
    {
        btn_hangOff.setVisibility(View.GONE);
        btn_reject.setVisibility(View.VISIBLE);
    }

    void switchToInCallMode()
    {
        btn_hangOff.setVisibility(View.VISIBLE);
        btn_reject.setVisibility(View.GONE);
    }


    void initView()
    {
        btn_answer = (Button)findViewById(R.id.btn_answer);
        btn_hangOff = (Button)findViewById(R.id.btn_hangoff);
        btn_reject = (Button)findViewById(R.id.btn_reject);
        text_info = (TextView)findViewById(R.id.text_info);


        findViewById(R.id.btn_back).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(GcordSDK.getInstance().getPhoneAPI().isInCall())
                {
                    GcordSDK.getInstance().getPhoneAPI().hangOff();
                }
                finish();
            }
        });

        btn_reject.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                GcordSDK.getInstance().getPhoneAPI().reject();
                finish();
            }
        });

        btn_answer.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                GcordSDK.getInstance().getPhoneAPI().pickUpWithHandsFree();
            }
        });

        btn_hangOff.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                GcordSDK.getInstance().getPhoneAPI().hangOff();
            }
        });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        stopPlayingRing();
    }


    PhoneAPI.PhoneEventListener phoneEventListener = new PhoneAPI.PhoneEventListener() {
        @Override
        public void onPickUp(PhoneAPI.PICKUP_STATE pickup_state) {
            stopPlayingRing();
            switchToInCallMode();
        }

        @Override
        public void onInComingCall() {
            text_info.append("收到新的来电\n");
        }

        @Override
        public void onRingEnd() {
            text_info.append("响铃结束\n");
            if(!GcordSDK.getInstance().getPhoneAPI().isInCall())
            {
                finish();
            }
        }

        @Override
        public void onPhoneNumberReceived(String s) {
            text_info.append("收到来电号码:"+s + "\n");
        }

        @Override
        public void onSwitchPhoneState(PhoneAPI.PICKUP_STATE pickup_state) {
            if(pickup_state == PhoneAPI.PICKUP_STATE.HANDLE)
            {
                text_info.append("切换至手柄通话\n");
            }
            else
            {
                text_info.append("切换至免提通话\n");
            }
        }

        @Override
        public void onHangOff() {
            text_info.append("挂机");
            finish();
        }
    };


    void playRing()
    {

    }

    void stopPlayingRing()
    {

    }

}
